
package com.wyzz.hr.action;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpRelationsBo;
import com.wyzz.hr.pojo.Emprelations;
import com.wyzz.hr.spring.SpringBeanFactory;

public class EmpRelationsDWR extends BaseAction {

	public EmpRelationsDWR() {
	}

	public String relationsAdd() throws Exception {
		if ("self".equals(emprelations.getEmployee().getId()))
			emprelations.setEmployee(getCurrentEmp());
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(emprelations.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u589E\u52A0\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		} else {
			empRelationsBo = (IEmpRelationsBo) SpringBeanFactory
					.getBean("empRelationsBo");
			emprelations.setEmployee(emprelations.getEmployee());
			emprelations.setErelCreateBy(getCurrentEmpNo());
			emprelations.setErelCreateDate(new Date());
			emprelations.setErelLastChangeBy(getCurrentEmpNo());
			emprelations.setErelLastChangeTime(new Date());
			empRelationsBo.insert(emprelations);
			addActionMessage("\u793E\u4F1A\u5173\u7CFB\u589E\u52A0\u6210\u529F\u3002");
			return "success";
		}
	}

	public String relationsDel(String erelId) {
		String auth = DWRUtil.checkAuth("emprelationsdwr", "");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		empRelationsBo = (IEmpRelationsBo) SpringBeanFactory
				.getBean("empRelationsBo");
		String fetches[] = { Emprelations.PROP_EREL_EMPLOYEE };
		Emprelations empOldRelations = empRelationsBo.load(erelId, fetches);
		if ("SUB".equals(auth)
				&& !checkAuth(empOldRelations.getEmployee().getId())) {
			return "noauth";
		} else {
			empRelationsBo.delete(empOldRelations);
			return "success";
		}
	}

	public String relationsUpdate() throws Exception {
		empRelationsBo = (IEmpRelationsBo) SpringBeanFactory
				.getBean("empRelationsBo");
		List list = empRelationsBo.search(emprelations);
		if (list.size() == 0)
			return "error";
		Emprelations empOldRelations = (Emprelations) list.get(0);
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(empOldRelations.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		} else {
			empOldRelations.setErelRelationship(emprelations
					.getErelRelationship());
			empOldRelations.setErelName(emprelations.getErelName());
			empOldRelations.setErelBirthday(emprelations.getErelBirthday());
			empOldRelations.setErelBirthdayRemind(emprelations
					.getErelBirthdayRemind());
			empOldRelations.setErelCompany(emprelations.getErelCompany());
			empOldRelations.setErelPosition(emprelations.getErelPosition());
			empOldRelations.setErelContactInfo(emprelations
					.getErelContactInfo());
			empOldRelations.setErelLastChangeBy(getCurrentEmpNo());
			empOldRelations.setErelLastChangeTime(new Date());
			empRelationsBo.update(empOldRelations);
			addSuccessInfo("\u4FEE\u6539\u5458\u5DE5\u793E\u4F1A\u5173\u7CFB\u6210\u529F\u3002");
			return "success";
		}
	}

	public Emprelations loadTrainHis(String ehrId) {
		if (StringUtils.isEmpty(ehrId)) {
			return null;
		} else {
			empRelationsBo = (IEmpRelationsBo) SpringBeanFactory
					.getBean("empRelationsBo");
			Emprelations result = empRelationsBo.load(ehrId, new String[0]);
			return result;
		}
	}

	public Emprelations getEmprelations() {
		return emprelations;
	}

	public void setEmprelations(Emprelations emprelations) {
		this.emprelations = emprelations;
	}

	public String getErelId() {
		return erelId;
	}

	public void setErelId(String erelId) {
		this.erelId = erelId;
	}

	private static final long serialVersionUID = 1L;
	private Emprelations emprelations;
	private IEmpRelationsBo empRelationsBo;
	private String erelId;
}
